#include "deletefriend.h"
#include "ui_deletefriend.h"
#include "user.h"
deletefriend::deletefriend(QWidget *parent,user userSelf,QString theOtherId,QString theOtherName) :
    QDialog(parent),
    ui(new Ui::deletefriend)
{
    ui->setupUi(this);
    this->userSelf=userSelf;
    ui->textBrowser_id->append(theOtherId);
    ui->textBrowser_name->append(theOtherName);
    otherid=theOtherId;
    connect(ui->send,&QPushButton::clicked,this, &deletefriend:: send_clicked);//删除好友
    connect(ui->cancel,&QPushButton::clicked,this, &deletefriend:: cancel_clicked);//取消
}

deletefriend::~deletefriend()
{
    delete ui;
}
void deletefriend::send_clicked()
{
    QJsonObject json2{//发送给后端的添加人
        {"id", userSelf.getUserId().toInt()},
        {"token", userSelf.getUserPassword()},
        {"the_other_id",otherid.toInt()}
    };
    qDebug()<<json2;


    QString resourcePath = ":/asset/config/config.json";
    QFile jsonFile(resourcePath);
    if (jsonFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QByteArray jsonData = jsonFile.readAll();
    // 将 JSON 数据转换为 QVariant 对象
    QJsonParseError error;
    QJsonObject result = QJsonDocument::fromJson(jsonData, &error).object();
    QJsonObject jsonUrl = QJsonDocument::fromJson(jsonData).object();
    qDebug() << "url :" << jsonUrl;
    jsonFile.close();
    QVariantMap urlVariantMap = jsonUrl.toVariantMap();
    QMap urlMap = urlVariantMap.value("server").toMap();
    host = urlMap.value("host").toString();
    port = urlMap.value("port").toString();
    }

    QNetworkRequest request(QUrl("http://"+host+':'+port+"/slimarveous/delete_friend"));
    //QNetworkRequest request(QUrl("http://localhost:19999/slimarveous/delete_friend"));



    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json2");
    //QJsonObject json2;
    QJsonDocument doc(json2);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    QNetworkReply *reply = manager.post(request, data);
    //qDebug()<<"data"<<data;
    connect(reply, &QNetworkReply::finished, [=](){});
    this->close();
    QMessageBox:: information(this, "删除好友","你已删除id为：" +otherid+"的好友！请刷新好友列表");
    //跳转到主界面
}
void deletefriend::cancel_clicked()
{
    this->close();
}
